package main

import "fmt"

func main() {
	var name string
	name = "duke"
	fmt.Println(name)

	//定义直接赋值，自动推导（常用）
	address := "beijing"
	fmt.Println(address)

	i, j := 10, 20
	fmt.Println(i, j)

	i, j = j, i
	fmt.Println(i, j)

	value := 2
	value++
	fmt.Println(value)

	mp := "caoxiansheng"
	ptr := &mp
	fmt.Println("name: ", *ptr)
	fmt.Println("name ptr:", ptr)

	mp2 := new(string)
	*mp2 = "hello world"
	fmt.Println("name: ", *mp2)
	fmt.Println("name ptr: ", mp2)
	fmt.Println("name len:", len(*mp2))

	for i := 0; i < len(mp); i++ {
		fmt.Printf("i: %d, v: %c\n", i, mp[i])
	}

	nums := [10]int{1, 2, 3, 4}
	for i := 0; i < len(nums); i++ {
		println("i: ", i, ", j:", nums[i])
	}

	for key, value := range nums {
		println(key, value)
	}

	//如何忽略一个值，可以使用_
	//如果两个都忽略，就不能使用 := ,而应该使用 =
	for _, _ = range nums {
		println("tetst ---")
	}

	dynamicNums := []string{"beijing", "shanghai", "guangzhou", "shenzhen"}
	for i, v := range dynamicNums {
		fmt.Println(i, v)
	}
	println("长度", len(dynamicNums), "容量:", cap(dynamicNums))

	dynamicNums = append(dynamicNums, "秦皇岛")
	for i, v := range dynamicNums {
		fmt.Println(i, v)
	}
	println("长度", len(dynamicNums), "容量:", cap(dynamicNums))

	dynamicNums2 := [3]string{}
	dynamicNums2[0] = dynamicNums[0]
	dynamicNums2[1] = dynamicNums[1]
	dynamicNums2[2] = dynamicNums[2]
	fmt.Println("dynamicNums2:", dynamicNums2)

	dynamicNums2[2] = "heze"
	fmt.Println("dynamicNums", dynamicNums)
	fmt.Println("dynamicNums2:", dynamicNums2)

	dynamicSplice := dynamicNums[0:2]
	fmt.Println("dynamicSplice:", dynamicSplice)

	dynamicSplice[1] = "jinan"
	fmt.Println("dynamicSplice:", dynamicSplice)
	fmt.Println("dynamicNums:", dynamicNums)

	//make
	//创建一个容量是20，长度是0的string类型切片
	strMake := make([]string, 0, 20)
	fmt.Println("长度：", len(strMake), "容量: ", cap(strMake))

	strMakeCopy := make([]string, len(dynamicNums))
	copy(strMakeCopy, dynamicNums[:]) //copy类型接收的参数类型是切片，所以需要使用[:]将数组变成切片
	strMakeCopy[0] = "北京"
	fmt.Printf("strMakeCopy: %s\n, dynamicNums: %s\n", strMakeCopy, dynamicNums)

}
